package cn.edu.cug.cs.gtl.apis.ui.datake.devicetree;

import cn.edu.cug.cs.gtl.apis.ui.AbstractApplication;
import cn.edu.cug.cs.gtl.apis.ui.datake.DatakeApp;
import cn.edu.cug.cs.gtl.apis.ui.datake.DatakeFrame;
import cn.edu.cug.cs.gtl.apis.ui.tree.Tree;
import cn.edu.cug.cs.gtl.apis.ui.tree.TreeMouseListener;
import cn.edu.cug.cs.gtl.apis.ui.tree.TreeNodeData;
import cn.edu.cug.cs.gtl.protos.*;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;

public class DeviceTreeMouseListener extends TreeMouseListener {

    public DeviceTreeMouseListener(Tree tree, AbstractApplication theApp) {
        super(tree, theApp);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        super.mouseClicked(e);
    }

    @Override
    public void mousePressed(MouseEvent e) {
        super.mousePressed(e);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        super.mouseReleased(e);

        if (SwingUtilities.isRightMouseButton(e)) {
            DeviceTree tree = (DeviceTree) (getDatakeFrame().getDevicePanel().getTreeComponent());
            int selRow = tree.getRowForLocation(e.getX(), e.getY());
            TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
            tree.setSelectionPath(selPath);
            if (selRow > -1) {
                tree.setSelectionRow(selRow);
            } else {
                tree.setSelectionRow(0);
            }
            //获取当前结点，若为集群的叶子结点，则判断类型是StorageCluster...，若不是叶子结点，则获取父结点的refObject数据
            DefaultMutableTreeNode currentSelectedNode = tree.getCurrentSelectedNode();
            Object refObject = ((TreeNodeData) currentSelectedNode.getUserObject()).getRefObject();

            if(refObject instanceof StorageCluster){
                JPopupMenu popupMenu = tree.getStorageChildNodePopupMenu();
                popupMenu.show(e.getComponent(), e.getX(), e.getY());
            }else{
                //获取父节点的refObject数据
                DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) currentSelectedNode.getParent();

                if (parentNode != null) {
                    TreeNodeData userObject = (TreeNodeData) parentNode.getUserObject();
                    Object obj = userObject.getRefObject();
                    if (obj == null) return;
                    if (obj.equals("Storage Cluster") || obj.equals("存储集群")) {
                        //通过插件给右键菜单添加了菜单项后的右键菜单
                        JPopupMenu popupMenu = tree.getStorageClusterNodePopupMenu();
                        //popupMenu.add(tree.getPopupMenu());
                        //tree.setStorageClusterNodePopupMenu(popupMenu);
                        popupMenu.show(e.getComponent(), e.getX(), e.getY());
                    } else if (obj.equals("Computing Cluster") || obj.equals("计算集群")) {
                        JPopupMenu popupMenu = tree.getComputingClusterNodePopupMenu();
                        //tree.setComputingClusterNodePopupMenu(popupMenu);
                        popupMenu.show(e.getComponent(), e.getX(), e.getY());
                    } else if (obj.equals("Search Cluster") || obj.equals("搜索集群")) {
                        JPopupMenu popupMenu = tree.getSearchClusterNodePopupMenu();
                        //tree.setSearchClusterNodePopupMenu(popupMenu);
                        popupMenu.show(e.getComponent(), e.getX(), e.getY());
                    } else if (obj.equals("Message Cluster") || obj.equals("消息集群")) {
                        JPopupMenu popupMenu = tree.getMessageClusterNodePopupMenu();
                        //tree.setMessageClusterNodePopupMenu(popupMenu);
                        popupMenu.show(e.getComponent(), e.getX(), e.getY());
                    } else if (obj.equals("Cache Cluster") || obj.equals("缓存集群")) {
                        JPopupMenu popupMenu = tree.getCacheClusterNodePopupMenu();
                        //tree.setCacheClusterNodePopupMenu(popupMenu);
                        popupMenu.show(e.getComponent(), e.getX(), e.getY());
                    }else {
                        JPopupMenu popupMenu = tree.getPopupMenu();
                        popupMenu.show(e.getComponent(), e.getX(), e.getY());
                    }
                } else {
                    JPopupMenu popupMenu = tree.getPopupMenu();
                    popupMenu.show(e.getComponent(), e.getX(), e.getY());
                }
            }

        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        super.mouseEntered(e);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        super.mouseExited(e);
    }

    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        super.mouseWheelMoved(e);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        super.mouseDragged(e);
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        super.mouseMoved(e);
    }

    public DatakeFrame getDatakeFrame() {
        return (DatakeFrame) (((DatakeApp)theApp).getMainFrame());
    }
}
